.class public Lorg/microemu/app/util/MIDletThread;
.super Ljava/lang/Thread;
.source "MIDletThread.java"


# static fields
.field private static final THREAD_NAME_PREFIX:Ljava/lang/String; = "MIDletThread-"

.field static class$org$microemu$app$util$MIDletThread:Ljava/lang/Class;

.field public static graceTerminationPeriod:I

.field private static midlets:Ljava/util/Map;

.field private static terminator:Z

.field private static threadInitNumber:I


# instance fields
.field private callLocation:Ljava/lang/String;


# direct methods
.method static constructor <clinit>()V
    .locals 1

    .prologue
    .line 45
    const/16 v0, 0x1388

    sput v0, Lorg/microemu/app/util/MIDletThread;->graceTerminationPeriod:I

    .line 49
    const/4 v0, 0x0

    sput-boolean v0, Lorg/microemu/app/util/MIDletThread;->terminator:Z

    .line 51
    new-instance v0, Ljava/util/WeakHashMap;

    invoke-direct {v0}, Ljava/util/WeakHashMap;-><init>()V

    sput-object v0, Lorg/microemu/app/util/MIDletThread;->midlets:Ljava/util/Map;

    return-void
.end method

.method public constructor <init>()V
    .locals 2

    .prologue
    .line 62
    new-instance v0, Ljava/lang/StringBuffer;

    invoke-direct {v0}, Ljava/lang/StringBuffer;-><init>()V

    const-string v1, "MIDletThread-"

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-static {}, Lorg/microemu/app/util/MIDletThread;->nextThreadNum()I

    move-result v1

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(I)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {p0, v0}, Ljava/lang/Thread;-><init>(Ljava/lang/String;)V

    .line 63
    invoke-static {p0}, Lorg/microemu/app/util/MIDletThread;->register(Lorg/microemu/app/util/MIDletThread;)V

    .line 64
    return-void
.end method

.method public constructor <init>(Ljava/lang/Runnable;)V
    .locals 2
    .parameter "target"

    .prologue
    .line 67
    new-instance v0, Ljava/lang/StringBuffer;

    invoke-direct {v0}, Ljava/lang/StringBuffer;-><init>()V

    const-string v1, "MIDletThread-"

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-static {}, Lorg/microemu/app/util/MIDletThread;->nextThreadNum()I

    move-result v1

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(I)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {p0, p1, v0}, Ljava/lang/Thread;-><init>(Ljava/lang/Runnable;Ljava/lang/String;)V

    .line 68
    invoke-static {p0}, Lorg/microemu/app/util/MIDletThread;->register(Lorg/microemu/app/util/MIDletThread;)V

    .line 69
    return-void
.end method

.method public constructor <init>(Ljava/lang/Runnable;Ljava/lang/String;)V
    .locals 2
    .parameter "target"
    .parameter "name"

    .prologue
    .line 72
    new-instance v0, Ljava/lang/StringBuffer;

    invoke-direct {v0}, Ljava/lang/StringBuffer;-><init>()V

    const-string v1, "MIDletThread-"

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0, p2}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {p0, p1, v0}, Ljava/lang/Thread;-><init>(Ljava/lang/Runnable;Ljava/lang/String;)V

    .line 73
    invoke-static {p0}, Lorg/microemu/app/util/MIDletThread;->register(Lorg/microemu/app/util/MIDletThread;)V

    .line 74
    return-void
.end method

.method public constructor <init>(Ljava/lang/String;)V
    .locals 2
    .parameter "name"

    .prologue
    .line 77
    new-instance v0, Ljava/lang/StringBuffer;

    invoke-direct {v0}, Ljava/lang/StringBuffer;-><init>()V

    const-string v1, "MIDletThread-"

    invoke-virtual {v0, v1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0, p1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v0

    invoke-virtual {v0}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {p0, v0}, Ljava/lang/Thread;-><init>(Ljava/lang/String;)V

    .line 78
    invoke-static {p0}, Lorg/microemu/app/util/MIDletThread;->register(Lorg/microemu/app/util/MIDletThread;)V

    .line 79
    return-void
.end method

.method static access$000(Ljava/util/Map;)V
    .locals 0
    .parameter "x0"

    .prologue
    .line 43
    invoke-static {p0}, Lorg/microemu/app/util/MIDletThread;->terminateThreads(Ljava/util/Map;)V

    return-void
.end method

.method static class$(Ljava/lang/String;)Ljava/lang/Class;
    .locals 2
    .parameter "x0"

    .prologue
    .line 87
    :try_start_0
    invoke-static {p0}, Ljava/lang/Class;->forName(Ljava/lang/String;)Ljava/lang/Class;
    :try_end_0
    .catch Ljava/lang/ClassNotFoundException; {:try_start_0 .. :try_end_0} :catch_0

    move-result-object v1

    return-object v1

    :catch_0
    move-exception v0

    .local v0, x1:Ljava/lang/ClassNotFoundException;
    new-instance v1, Ljava/lang/NoClassDefFoundError;

    invoke-direct {v1}, Ljava/lang/NoClassDefFoundError;-><init>()V

    invoke-virtual {v1, v0}, Ljava/lang/NoClassDefFoundError;->initCause(Ljava/lang/Throwable;)Ljava/lang/Throwable;

    move-result-object v1

    throw v1
.end method

.method public static contextDestroyed(Lorg/microemu/MIDletContext;)V
    .locals 3
    .parameter "midletContext"

    .prologue
    .line 111
    if-nez p0, :cond_0

    .line 125
    :goto_0
    return-void

    .line 114
    :cond_0
    sget-object v2, Lorg/microemu/app/util/MIDletThread;->midlets:Ljava/util/Map;

    invoke-interface {v2, p0}, Ljava/util/Map;->remove(Ljava/lang/Object;)Ljava/lang/Object;

    move-result-object v1

    check-cast v1, Ljava/util/Map;

    .line 115
    .local v1, threads:Ljava/util/Map;
    if-eqz v1, :cond_1

    invoke-interface {v1}, Ljava/util/Map;->size()I

    move-result v2

    if-eqz v2, :cond_1

    .line 116
    const/4 v2, 0x1

    sput-boolean v2, Lorg/microemu/app/util/MIDletThread;->terminator:Z

    .line 117
    new-instance v0, Lorg/microemu/app/util/MIDletThread$1;

    const-string v2, "MIDletThreadsTerminator"

    invoke-direct {v0, v2, v1}, Lorg/microemu/app/util/MIDletThread$1;-><init>(Ljava/lang/String;Ljava/util/Map;)V

    .line 122
    .local v0, terminator:Ljava/lang/Thread;
    invoke-virtual {v0}, Ljava/lang/Thread;->start()V

    .line 124
    .end local v0           #terminator:Ljava/lang/Thread;
    :cond_1
    invoke-static {p0}, Lorg/microemu/app/util/MIDletTimer;->contextDestroyed(Lorg/microemu/MIDletContext;)V

    goto :goto_0
.end method

.method public static hasRunningThreads(Lorg/microemu/MIDletContext;)Z
    .locals 1
    .parameter "midletContext"

    .prologue
    .line 129
    sget-boolean v0, Lorg/microemu/app/util/MIDletThread;->terminator:Z

    return v0
.end method

.method private static declared-synchronized nextThreadNum()I
    .locals 3

    .prologue
    .line 58
    const-class v0, Lorg/microemu/app/util/MIDletThread;

    monitor-enter v0

    :try_start_0
    sget v1, Lorg/microemu/app/util/MIDletThread;->threadInitNumber:I

    add-int/lit8 v2, v1, 0x1

    sput v2, Lorg/microemu/app/util/MIDletThread;->threadInitNumber:I
    :try_end_0
    .catchall {:try_start_0 .. :try_end_0} :catchall_0

    monitor-exit v0

    return v1

    :catchall_0
    move-exception v1

    monitor-exit v0

    throw v1
.end method

.method private static register(Lorg/microemu/app/util/MIDletThread;)V
    .locals 4
    .parameter "thread"

    .prologue
    .line 82
    invoke-static {}, Lorg/microemu/MIDletBridge;->getMIDletContext()Lorg/microemu/MIDletContext;

    move-result-object v0

    .line 83
    .local v0, midletContext:Lorg/microemu/MIDletContext;
    if-nez v0, :cond_0

    .line 84
    const-string v2, "Creating thread with no MIDlet context"

    new-instance v3, Ljava/lang/Throwable;

    invoke-direct {v3}, Ljava/lang/Throwable;-><init>()V

    invoke-static {v2, v3}, Lorg/microemu/log/Logger;->error(Ljava/lang/String;Ljava/lang/Throwable;)V

    .line 94
    :goto_0
    return-void

    .line 87
    :cond_0
    sget-object v2, Lorg/microemu/app/util/MIDletThread;->class$org$microemu$app$util$MIDletThread:Ljava/lang/Class;

    if-nez v2, :cond_2

    const-string v2, "org.microemu.app.util.MIDletThread"

    invoke-static {v2}, Lorg/microemu/app/util/MIDletThread;->class$(Ljava/lang/String;)Ljava/lang/Class;

    move-result-object v2

    sput-object v2, Lorg/microemu/app/util/MIDletThread;->class$org$microemu$app$util$MIDletThread:Ljava/lang/Class;

    :goto_1
    invoke-virtual {v2}, Ljava/lang/Class;->getName()Ljava/lang/String;

    move-result-object v2

    invoke-static {v2}, Lorg/microemu/util/ThreadUtils;->getCallLocation(Ljava/lang/String;)Ljava/lang/String;

    move-result-object v2

    iput-object v2, p0, Lorg/microemu/app/util/MIDletThread;->callLocation:Ljava/lang/String;

    .line 88
    sget-object v2, Lorg/microemu/app/util/MIDletThread;->midlets:Ljava/util/Map;

    invoke-interface {v2, v0}, Ljava/util/Map;->get(Ljava/lang/Object;)Ljava/lang/Object;

    move-result-object v1

    check-cast v1, Ljava/util/Map;

    .line 89
    .local v1, threads:Ljava/util/Map;
    if-nez v1, :cond_1

    .line 90
    new-instance v1, Ljava/util/WeakHashMap;

    .end local v1           #threads:Ljava/util/Map;
    invoke-direct {v1}, Ljava/util/WeakHashMap;-><init>()V

    .line 91
    .restart local v1       #threads:Ljava/util/Map;
    sget-object v2, Lorg/microemu/app/util/MIDletThread;->midlets:Ljava/util/Map;

    invoke-interface {v2, v0, v1}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    .line 93
    :cond_1
    invoke-interface {v1, p0, v0}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    goto :goto_0

    .line 87
    .end local v1           #threads:Ljava/util/Map;
    :cond_2
    sget-object v2, Lorg/microemu/app/util/MIDletThread;->class$org$microemu$app$util$MIDletThread:Ljava/lang/Class;

    goto :goto_1
.end method

.method private static terminateThreads(Ljava/util/Map;)V
    .locals 11
    .parameter "threads"

    .prologue
    .line 133
    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v7

    sget v9, Lorg/microemu/app/util/MIDletThread;->graceTerminationPeriod:I

    int-to-long v9, v9

    add-long v2, v7, v9

    .line 134
    .local v2, endTime:J
    invoke-interface {p0}, Ljava/util/Map;->keySet()Ljava/util/Set;

    move-result-object v7

    invoke-interface {v7}, Ljava/util/Set;->iterator()Ljava/util/Iterator;

    move-result-object v4

    .local v4, iter:Ljava/util/Iterator;
    :cond_0
    :goto_0
    invoke-interface {v4}, Ljava/util/Iterator;->hasNext()Z

    move-result v7

    if-eqz v7, :cond_4

    .line 135
    invoke-interface {v4}, Ljava/util/Iterator;->next()Ljava/lang/Object;

    move-result-object v5

    .line 136
    .local v5, o:Ljava/lang/Object;
    if-eqz v5, :cond_0

    .line 139
    instance-of v7, v5, Lorg/microemu/app/util/MIDletThread;

    if-eqz v7, :cond_3

    .line 140
    move-object v0, v5

    check-cast v0, Lorg/microemu/app/util/MIDletThread;

    move-object v6, v0

    .line 141
    .local v6, t:Lorg/microemu/app/util/MIDletThread;
    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->isAlive()Z

    move-result v7

    if-eqz v7, :cond_0

    .line 142
    new-instance v7, Ljava/lang/StringBuffer;

    invoke-direct {v7}, Ljava/lang/StringBuffer;-><init>()V

    const-string v8, "wait thread ["

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->getName()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    const-string v8, "] end"

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Lorg/microemu/log/Logger;->info(Ljava/lang/String;)V

    .line 143
    :goto_1
    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v7

    cmp-long v7, v2, v7

    if-lez v7, :cond_1

    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->isAlive()Z

    move-result v7

    if-eqz v7, :cond_1

    .line 145
    const-wide/16 v7, 0x2bc

    :try_start_0
    invoke-virtual {v6, v7, v8}, Lorg/microemu/app/util/MIDletThread;->join(J)V
    :try_end_0
    .catch Ljava/lang/InterruptedException; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_1

    .line 146
    :catch_0
    move-exception v1

    .line 150
    :cond_1
    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->isAlive()Z

    move-result v7

    if-eqz v7, :cond_0

    .line 151
    new-instance v7, Ljava/lang/StringBuffer;

    invoke-direct {v7}, Ljava/lang/StringBuffer;-><init>()V

    const-string v8, "MIDlet thread ["

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->getName()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    const-string v8, "] still running"

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-static {v6}, Lorg/microemu/util/ThreadUtils;->getTreadStackTrace(Ljava/lang/Thread;)Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Lorg/microemu/log/Logger;->warn(Ljava/lang/String;)V

    .line 152
    iget-object v7, v6, Lorg/microemu/app/util/MIDletThread;->callLocation:Ljava/lang/String;

    if-eqz v7, :cond_2

    .line 153
    new-instance v7, Ljava/lang/StringBuffer;

    invoke-direct {v7}, Ljava/lang/StringBuffer;-><init>()V

    const-string v8, "this thread ["

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->getName()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    const-string v8, "] was created from "

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    iget-object v8, v6, Lorg/microemu/app/util/MIDletThread;->callLocation:Ljava/lang/String;

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Lorg/microemu/log/Logger;->info(Ljava/lang/String;)V

    .line 155
    :cond_2
    invoke-virtual {v6}, Lorg/microemu/app/util/MIDletThread;->interrupt()V

    goto/16 :goto_0

    .line 159
    .end local v6           #t:Lorg/microemu/app/util/MIDletThread;
    :cond_3
    new-instance v7, Ljava/lang/StringBuffer;

    invoke-direct {v7}, Ljava/lang/StringBuffer;-><init>()V

    const-string v8, "unrecognized Object ["

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v5}, Ljava/lang/Object;->getClass()Ljava/lang/Class;

    move-result-object v8

    invoke-virtual {v8}, Ljava/lang/Class;->getName()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    const-string v8, "]"

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Lorg/microemu/log/Logger;->debug(Ljava/lang/String;)V

    goto/16 :goto_0

    .line 162
    .end local v5           #o:Ljava/lang/Object;
    :cond_4
    new-instance v7, Ljava/lang/StringBuffer;

    invoke-direct {v7}, Ljava/lang/StringBuffer;-><init>()V

    const-string v8, "all "

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-interface {p0}, Ljava/util/Map;->size()I

    move-result v8

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(I)Ljava/lang/StringBuffer;

    move-result-object v7

    const-string v8, " thread(s) finished"

    invoke-virtual {v7, v8}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Lorg/microemu/log/Logger;->debug(Ljava/lang/String;)V

    .line 163
    const/4 v7, 0x0

    sput-boolean v7, Lorg/microemu/app/util/MIDletThread;->terminator:Z

    .line 164
    return-void
.end method


# virtual methods
.method public run()V
    .locals 2

    .prologue
    .line 99
    :try_start_0
    invoke-super {p0}, Ljava/lang/Thread;->run()V
    :try_end_0
    .catch Ljava/lang/Throwable; {:try_start_0 .. :try_end_0} :catch_0

    .line 104
    :goto_0
    return-void

    .line 100
    :catch_0
    move-exception v0

    .line 101
    .local v0, e:Ljava/lang/Throwable;
    const-string v1, "MIDletThread throw"

    invoke-static {v1, v0}, Lorg/microemu/log/Logger;->debug(Ljava/lang/String;Ljava/lang/Throwable;)V

    goto :goto_0
.end method
